<?php

class Login_Model extends Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * Try to login user 
     */
    function Userlogin() {
        if (UserAccess::confirmUser($_POST['username'], $_POST['password'])) {
            $memberId = UserAccess::getMemberId($_POST['username'], $_POST['password']);

            UserAccess::createsessions($memberId, $_POST['username'], $_POST['password'], $_POST['rememberMe']);
            
            $this->UpdateUserPicks($_SESSION['memberId']);
            header('location: ' . URL . 'picks');
        }
        else
            header('location: ' . URL . 'login');
    }
    
    function GetMemberId($username) {
        //Get memberID from Members
        $sth = $this->database->prepare("SELECT memberId FROM Members WHERE username=:username");
        $sth->execute(array(
            ':username' => $username,
        ));
        
        $memberId = $sth->fetchColumn();
        
        return $memberId;
    }
    

    
 
    /**
     * Change Viwer to Mobile viewer 
     */
    function SetMobileViewer() {
        UserAccess::SetMobileViewer(true);
        header('Location: ' . URL);
    }

    /**
     * Change Viwer to Desktop viewer 
     */
    function SetDesktopViewer() {
        UserAccess::SetMobileViewer(false);
        header('Location: ' . URL);
    }


}
